﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DuJianliang.ApplicationPlatform.Common;
using DuJianliang.ApplicationPlatform.UserPermission;

namespace DuJianliang.ApplicationPlatform.RemoveItems
{
    public partial class RemoveItemsInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                UserPermissionBll userPermissionBll = new UserPermissionBll();
                if (!userPermissionBll.HasPermission(CurrentInfo.Instance.CurrentUser.ID, (int)Permission.RemoveItems))
                {
                    Response.Write("您没有权限使用该功能");
                    Response.End();
                    return;
                }
                BindGridView();
            }
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            Response.Redirect("RemoveItemsEdit.aspx");
        }

        private void BindGridView()
        {
            string sql = @"SELECT * FROM dbo.t5715_RemoveItems ORDER BY CreateDate DESC";

            Database database = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = database.GetSqlStringCommand(sql);
            DataTable dataTable = database.ExecuteDataSet(dbCommand).Tables[0];

            gridView.DataSource = dataTable;
            gridView.DataBind();
        }

        private void Delete(string id)
        {
            string sql_delete = @"DELETE FROM dbo.t5715_RemoveItems WHERE ID = @ID";

            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql_delete);
                database.AddInParameter(dbCommand, "@ID", DbType.String, id);
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void gridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(e.CommandName) || e.CommandName.ToLower().Equals("page"))
            {
                return;
            }

            GridViewRow gridViewRow = ((GridViewRow)(((Button)(e.CommandSource)).Parent.Parent));
            string id = gridView.DataKeys[gridViewRow.RowIndex]["ID"].ToString();

            switch (e.CommandName)
            {
                case "CmdEdit":
                    Response.Redirect(string.Format("RemoveItemsEdit.aspx?ID={0}", id));                    
                    break;
                case "CmdDelete":
                    this.Delete(id);
                    BindGridView();
                    break;
                default:
                    break;
            }
        }

        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindGridView();
        }        
    }
}